<?php
App::import('Model', 'products.Product');
class SearchController extends AppController {
	var $name = 'Search';
	var $uses = array('Product');
	/**
	 @var $model
	 @brief Thông tin về model, được sử dụng trong trường hợp override thông tin của danh mục
	 */
	var $models = array(
		'Category'	=>	array(
			'plugin'	=>	'products',
			'name'		=>	'ProductCategory'
		),
		'Page'		=>	array(
			'plugin'	=>	'products',
			'name'		=>	'Product'
		)
	);
	function beforeFilter() {
		parent::beforeFilter();
		
		// save contact us information
		$this->configs = $this->{$this->models['Page']['name']}->loadConfig();
		$this->configs = $this->{$this->models['Page']['name']}->mergeConfig($this->configs, array(
			'config_folder'		=>	$this->models['Page']['plugin'],
			'names'				=> array($this->models['Page']['name'], 'RelatedImages', 'Category', 'Appearence'),
			'overrides'			=>	array(
				'EditablePage'	=>	$this->models['Page']['name']
			)
		));		
		$this->set('configs_products', $this->configs);
	}
	
	

	function doSearch() {
		$keyword = '';
        if(isset($this->data['Search']['key']))  {     
            $keyword = $this->data['Search']['key'];             
        }
		$tmps	=	$this->{$this->models['Page']['name']}->find('all',array(
						'conditions'	=>	array(
							'OR'	=>	array(
								"I18n__title.content like '%{$keyword}%'",
								"I18n__description.content like '%{$keyword}%'",
								"I18n__content.content like '%{$keyword}%'"
							)
						),
						'fields'	=>	array('title', 'alias', 'id')
					));
		$ids = array(); 
		foreach ($tmps as $tmp) {
			$ids[]	=	$tmp["{$this->models['Page']['name']}"]['id'];
		}		
		$this->paginate = array(
			'conditions'	=>	array("Product.id"=>$ids),
			'fields'	=>	array('title', 'alias', 'image', 'description'),
			'contain' => array('ExtraProductField', 'Category'), 
			'limit' => 8
		);
		$products = $this->paginate('Product');
		$this->set('products', $products);
		//debug($products);
		$this->render('search_result');
    }
    
}
?>